import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Query,
} from '@nestjs/common';
import { StudycontentService } from './studycontent.service';
import { CreateStudycontentDto } from './dto/create-studycontent.dto';
import { UpdateStudycontentDto } from './dto/update-studycontent.dto';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { BaseController } from 'src/common/BaseController';
import { Studycontent } from './entities/studycontent.entity';
import { QueryListDto } from 'src/common/dto';
import { StudycontentQueryDto } from './dto/selectone-studycontent.dto';

@ApiTags('学习内容管理')
@Controller('studycontent')
export class StudycontentController extends BaseController<
  Studycontent,
  StudycontentService
> {
  constructor(private readonly studycontentService: StudycontentService) {
    super(studycontentService);
  }

  @Post('AddContent')
  @ApiOperation({ summary: '添加学习内容' })
  async addContent(@Body() StudyContent: CreateStudycontentDto) {
    return this.Success(
      this.studycontentService.create(StudyContent),
      '添加成功',
    );
  }

  @Get('GetContent')
  @ApiOperation({ summary: '获取所有学习内容' })
  async findAll(@Query() querylistDto: StudycontentQueryDto) {
    return this.Success(
      await this.studycontentService.findAll(querylistDto),
      '获取成功',
    );
  }

  @Get(':id')
  @ApiOperation({ summary: '根据 ID 获取学习内容' })
  async findOne(@Param('id') id: string) {
    // return this.Success(
    //   await this.studycontentService.findOne(+id),
    //   '获取成功',
    // );
    let result = await this.studycontentService.findOne(+id);
    if (result[0]) {
      return this.Success(result[1], '查询成功');
    } else {
      return this.Fail(result[1], '查询失败');
    }
  }

  @Patch(':id')
  @ApiOperation({ summary: '根据 ID 更新学习内容' })
  async update(
    @Param('id') id: string,
    @Body() updateStudycontentDto: UpdateStudycontentDto,
  ) {
    // return this.Success(
    //   await this.studycontentService.update(+id, updateStudycontentDto),
    //   '更新成功',
    // );
    let result = await this.studycontentService.update(
      +id,
      updateStudycontentDto,
    );
    if (result[0]) {
      return this.Success(result[1], '更新成功');
    } else {
      return this.Fail(result[1], '更新失败');
    }
  }

  @Delete(':id')
  @ApiOperation({ summary: '根据 ID 删除学习内容' })
  async remove(@Param('id') id: string) {
    return this.Success(await this.studycontentService.remove(+id), '删除成功');
  }
}
